using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for ContentManager
/// </summary>
public class ContentManager
{
    public enum SiteContentType { Unknown = 0, Resume , AboutMe, ProjectsSummary };

    #region gets
    public static List<ListItem> GetSiteContentTypes()
    {
        List<ListItem> lisReturnCollection = null;

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetSiteDataTypes", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        ListItem litTemItem;
                        lisReturnCollection = new List<ListItem>();
                        while (reader.Read())
                        {
                            litTemItem = new ListItem();

                            litTemItem.Value = reader["SDT_ID"].ToString();
                            litTemItem.Text = reader["SDT_NAME"].ToString();
                           
                            lisReturnCollection.Add(litTemItem);

                        }// end of the read while loop

                    }// end of the if check to see if anything was found
                }// end reader using              
            }// end command using
        }// end connection using

        return lisReturnCollection;
    }//end of the GetProjectStatuses method

    public static string GetSiteData(SiteContentType TypeOfData)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetSiteContentData", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@SDT_ID", (int)TypeOfData));                

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return reader["SCD_DATA"].ToString();
                       
                    }// end of the if check to see if anything was found
                }// end reader using              
            }// end command using
        }// end connection using

        return string.Empty;
    }// end of the GetSiteData method

    public static string GetSiteData(int ID)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetSiteContentData", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@SDT_ID", ID));

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return reader["SCD_DATA"].ToString();

                    }// end of the if check to see if anything was found
                }// end reader using              
            }// end command using
        }// end connection using

        return string.Empty;
    }// end of the GetSiteData method


    public static SiteData GetSiteDataWithFile(SiteContentType TypeOfData)
    {
        SiteData sdaDataCollection = new SiteData();
        sdaDataCollection.ContentType = TypeOfData;

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetSiteContentDataWithFile", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@SDT_ID", (int)TypeOfData));

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        sdaDataCollection.FileName = reader["FIL_NAME"].ToString();
                        sdaDataCollection.Data = reader["SCD_DATA"].ToString();
                        sdaDataCollection.FlieID = reader["SCD_FILE_FIL_ID"].ToString();
                    }// end of the if check to see if anything was found
                }// end reader using              
            }// end command using
        }// end connection using

        return sdaDataCollection;

    }// end of the GetSiteData method


    public static SiteData GetSiteDataWithFile(int ID)
    {
        SiteData sdaDataCollection = new SiteData();
        sdaDataCollection.ContentType = SiteContentType.Unknown;

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetSiteContentDataWithFile", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@SDT_ID", ID));

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        sdaDataCollection.FileName = reader["FIL_NAME"].ToString();
                        sdaDataCollection.Data = reader["SCD_DATA"].ToString();
                        sdaDataCollection.FlieID = reader["SCD_FILE_FIL_ID"].ToString();
                    }// end of the if check to see if anything was found
                }// end reader using              
            }// end command using
        }// end connection using

        return sdaDataCollection;

    }// end of the GetSiteData method
    #endregion

    #region updates

    public static bool UpdateSiteData(String Data, int ID)
    {
        int intRowsUpdated = 0;
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("UpdateSiteData", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@SCD_ID", ID));
                command.Parameters.Add(new SqlParameter("@SCD_DATA", Data));

                connection.Open();
                intRowsUpdated = command.ExecuteNonQuery();

            }// end command using
        }// end connection using

        return ((intRowsUpdated == 0) ? false : true);

    }// end of the UPdateSiteData method


    public static bool UpdateSiteData(String Data, SiteContentType TypeOfData)
    {
        int intRowsUpdated = 0;
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("UpdateSiteData", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@SCD_ID", (int)TypeOfData));
                command.Parameters.Add(new SqlParameter("@SCD_DATA", Data));

                connection.Open();
                intRowsUpdated = command.ExecuteNonQuery();   
        
                
            }// end command using
        }// end connection using

        return ((intRowsUpdated == 0) ? false : true);

    }// end of the UPdateSiteData method


    public static int UpdateSiteDataWithFile(String Data, int ID, int FileID)
    {
        int intRowsUpdated = 0;
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("UpdateSiteDataWithFile", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@SDT_ID", ID));
                command.Parameters.Add(new SqlParameter("@SDT_DATA", Data));
                command.Parameters.Add(new SqlParameter("@SDT_FILE_FIL_ID", FileID));

                connection.Open();
                intRowsUpdated = command.ExecuteNonQuery();

            }// end command using
        }// end connection using

        //return ((intRowsUpdated == 0) ? false : true);
        return intRowsUpdated;

    }// end of the UPdateSiteData method 

    public static int UpdateSiteDataWithFile(String Data, SiteContentType TypeOfData, int FileID)
    {
        int intRowsUpdated = 0;
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("UpdateSiteDataWithFile", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@SDT_ID", (int)TypeOfData));
                command.Parameters.Add(new SqlParameter("@SDT_DATA", Data));
                command.Parameters.Add(new SqlParameter("@SDT_FILE_FIL_ID", FileID));

                connection.Open();
                intRowsUpdated = command.ExecuteNonQuery();

            }// end command using
        }// end connection using

        //return ((intRowsUpdated == 0) ? false : true);
        return intRowsUpdated;

    }// end of the UPdateSiteData method 
    #endregion

}// end of the ContentManager class
